/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package midlet;

import canvas.MIDPCanvasEnd;
import canvas.MIDPCanvasHighScore;
import canvas.MIDPCanvasHowToPlay;
import canvas.MIDPCanvasMenu;
import canvas.MIDPCanvasOptions;
import canvas.MIDPCanvasPlay;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.media.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;
import thread.RunableSound;
import util.Convert;

/**
 * @author LTTDNQ
 */
public class MidletPlay extends MIDlet implements CommandListener {
    //Khai báo biến

    private Display _display;
    private Command _cmExit;
    private int _heightMobile = 320;
    private int _widthMobile = 240;
    private int _viTriMenu = 1;
    private boolean _stopMusic = false;
    private MIDPCanvasPlay _canvasPlay;
    private MIDPCanvasMenu _canvasMenu;
    private MIDPCanvasHowToPlay _canvasHow;
    private MIDPCanvasEnd _canvasEnd;
    private MIDPCanvasHighScore _canvasHS;
    private MIDPCanvasOptions _canvasOpt;
    private RunableSound _runableSound;
    private RecordStore _rs = null;
    private static final String REC_STORE = "db_hs";
    private Convert _classConvert;

    //Tạo contrustor cho class MidletPlay
    public MidletPlay() {
        //Hiện thị mặc định của màn hình
        _display = Display.getDisplay(this);
        _runableSound = new RunableSound(this);
        new Thread(_runableSound).start();
        _classConvert = new Convert();
    }
    //Khởi tạo get set

    public boolean getStopMusic() {
        return _stopMusic;
    }

    public int getHeightMobile() {
        return _heightMobile;
    }

    public int getViTriMenu() {
        return _viTriMenu;
    }

    public void setViTriMenu(int x) {
        this._viTriMenu = x;
    }

    public void setHeightMobile(int x) {
        this._heightMobile = x;
    }

    public int getWidthMobile() {
        return _widthMobile;
    }

    public void setWidthMobile(int x) {
        this._widthMobile = _widthMobile;
    }

    public Display getDisplay() {
        return _display;
    }

    public void setDisplay(Display x) {
        this._display = x;
    }

    public void vaoManHinhChoi() {
        _canvasPlay = new MIDPCanvasPlay(this);
        _cmExit = new Command("Exit", Command.OK, 1);
        _canvasPlay.addCommand(_cmExit);
        _canvasPlay.setCommandListener(this);
        _display.setCurrent(_canvasPlay);
    }

    public void vaoManHinhMenu() {
        _canvasMenu.paintMenu();
        _display.setCurrent(_canvasMenu);
    }

    public void vaoManHinhHowToPlay() {
        _canvasHow = new MIDPCanvasHowToPlay(this);
        _canvasHow.paintHowToPlay();
        _display.setCurrent(_canvasHow);
    }

    public void vaoManHinhOptions() {
        _canvasOpt = new MIDPCanvasOptions(this);
        _canvasOpt.paintOptions();
        _display.setCurrent(_canvasOpt);
    }

    public void vaoManHinhHighScore() {
        _canvasHS = new MIDPCanvasHighScore(this);
        _canvasHS.paintHighScore();
        _display.setCurrent(_canvasHS);
    }

    public void vaoManHinhKetThuc() {
        _canvasEnd = new MIDPCanvasEnd(this, _canvasPlay.getScore());
        _canvasPlay.destroy();
        System.out.println("Da null toan bo");
        _canvasEnd.paintEnd();
        _display.setCurrent(_canvasEnd);

    }

    //Phương thức start
    public void startApp() {
        _canvasMenu = new MIDPCanvasMenu(this);
        _canvasMenu.paintMenu();
        _display.setCurrent(_canvasMenu);
        // openRecordStore();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) throws MIDletStateChangeException {
        _stopMusic = true;
        _runableSound.tatNhac();
        _runableSound = null;
    }

    public void exit(int t) throws MIDletStateChangeException {
        if (t == 1) {
            _canvasPlay.destroy();
            vaoManHinhMenu();
        } else if (t == 2) {
            vaoManHinhMenu();
        } else {
            destroyApp(false);
            notifyDestroyed();
        }
    }

    public void commandAction(Command c, Displayable d) {
        if (c == _cmExit) {
            try {
                exit(1);
            } catch (MIDletStateChangeException ex) {

                System.out.println("Bi loi khi dong record");
                ex.printStackTrace();
            }
        }
    }
}
